home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 8787 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.9 KB  |  68 lines

  1. Path: bantolov.seanet.com!user
  2. From: bantolov@bantolov.seanet.com (Bruce Antolovich)
  3. Newsgroups: comp.lang.c
  4. Subject: Very Newbie question on pointers
  5. Date: Wed, 06 Mar 1996 00:18:08 -0800
  6. Organization: METRECON
  7. Message-ID: <bantolov-0603960018080001@bantolov.seanet.com>
  8. NNTP-Posting-Host: bantolov.seanet.com
  9. X-Newsreader: Yet Another NewsWatcher 2.0b30
  10.  
  11. I hope that this is the right place to post such a question; if not, sorry
  12. for the wasted bandspace.
  13.  
  14. I am very new to programming in C but have a large background in FORTRAN.
  15. (please no snickers) I am having a problem with the memory location of
  16. variables as described by pointers. I define several variables and try to
  17. get their address in memory. Unfortunately, my code gives me the same
  18. address for all variables! Any clues as to where I'm going wrong would be
  19. very appreciated.
  20.  
  21. I don't think that it matters but I'm using Metrowerks CW on a PowerMac 6100/60.
  22.  
  23. Thanks in advance
  24. Bruce
  25. bantolov@bantolov.seanet.com
  26.  
  27. Here is my sample code. (it does work correctly numerically)
  28.  
  29. #include <stdio.h>
  30.  
  31. /***********************/
  32. /* Function Prototypes */
  33. /***********************/
  34. void  SquareIt( int  number, int *squarePtr, int *cubePtr );
  35.  
  36. int main (void)
  37. {
  38.  
  39.    int square;
  40.    int cube;
  41.    int *myCube;
  42.    int *mySquare;
  43.    double test;
  44.    mySquare = □
  45.    myCube = &cube;
  46.    printf("%d \n",&test);
  47.    printf("The address of the variable square is %d. \n",mySquare);
  48.    printf("The address of the variable cube is %d. \n",myCube);
  49.    SquareIt( 5, &square, &cube );
  50.    printf( "5 squared is %d.\n", square );
  51.    printf( "5 cubed is %d. \n", cube );
  52.    return 0;
  53.  
  54. }
  55.  
  56. void SquareIt( int number, int *squarePtr, int *cubePtr)
  57. {
  58.    printf("squarePtr is %d. (the address of the calling variable)
  59. \n",squarePtr);
  60.    printf("cubePtr is %d. (the address of the calling variable) \n", cubePtr);
  61.  
  62.    *squarePtr = number * number;
  63.    printf("%d \n", *squarePtr);
  64.    *cubePtr = number * ( *squarePtr);
  65.  
  66.    
  67. }
  68.